package com.ddlaw.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ddlaw.entity.TLawThemes;
import com.ddlaw.entity.TThemesComments;
import com.ddlaw.service.TopicService;

/**
 * 
 *@author YunLong
 *@time 下午8:45:25
 */
@Controller
@RequestMapping(value = "/topic")
public class TopicController {
	
	@Resource
	TopicService topicService;
	
	@RequestMapping(value = "index/{typeId}", method = RequestMethod.GET)
	public String productListV(@PathVariable(value = "typeId") int typeId,Model model) {
		List<TLawThemes> lawThemes = topicService.getThemes(typeId);
		model.addAttribute("lawThemes", lawThemes);
		return "topic/index";
	}
	
	
	@RequestMapping(value = "comments/{themesId}", method = RequestMethod.GET)
	public String commentsList(@PathVariable(value = "themesId") int themesId,Model model) {
		TLawThemes lawThemes = topicService.getThemesById(themesId);
		List<TThemesComments> comments = topicService.getComments(themesId);
		model.addAttribute("comments", comments);
		model.addAttribute("lawThemes", lawThemes);
		return "topic/detail";
	}
	
	@RequestMapping(value = "remove/{commentId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> remove(@PathVariable(value = "commentId") int commentId) {
		Map<String, Object> result = new HashMap<String, Object>();
		int rs = topicService.delCommentById(commentId);
		if (rs > 0) {
			result.put("code", 200);
			result.put("msg", "删除成功！");
			return result;
		}
		result.put("code", -1);
		result.put("msg", "删除失败!请稍后再试！");
		return result;
	}
	
	
	@RequestMapping(value = "theme/del/{themeId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> delTheme(@PathVariable(value = "themeId") int themeId) {
		Map<String, Object> result = new HashMap<String, Object>();
		int rs = topicService.delThemeById(themeId);
		if (rs > 0) {
			result.put("code", 200);
			result.put("msg", "删除成功！");
			return result;
		}
		result.put("code", -1);
		result.put("msg", "删除失败!请稍后再试！");
		return result;
	}
	
}
